/**
 *  Copyright � 2010 Digia Plc
 *  Copyright � 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  �  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  �  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "javascript/sql.js" as HCS
import "../colibri"
import "styles"

Rectangle {
    id: mainStructure

    property real customScale: 1.00;
    property int xpos: 0;
    property int ypos: 0;
    property real customScale_temp: 1.00;
    property int xpos_temp: 0;
    property int ypos_temp: 0;
    property real customVisibility: 0;
    property real textCustomVisibility: 1;
    property bool iconHUDReload: true;

    property CLStyle style: StyleHomeControlSystem {}
    property CLStyle style1: StyleHomeControlSystem {}
    property CLStyle style2: StyleHomeControlSystem2 {}
    property CLStyle style3: StyleHomeControlSystem3 {}
    property CLStyle style4: StyleHomeControlSystem4 {}
    property CLStyle style5: StyleHomeControlSystem5 {}
    property CLStyle style6: StyleHomeControlSystem6 {}

    property bool roomAdjustWindowVisible : false


    property int theme : 0

    /**
     * Sets default values for demonstration
     *
     * @return Nothing
     */
    function startupFunction() {
        HCS.setDefaults();
    }

    /**
     * Changes HomeControlSystem theme
     *
     * @return Nothing
     */
    function changeCLStyle(){
        HCS.getTheme();
        theme = HCS.theme;
        if(theme == 1) style = style1;
        else if(theme == 2) style =  style2;
        else if(theme == 3) style =  style3;
        else if(theme == 4) style = style4;
        else if(theme == 5) style =  style5;
        else if(theme == 6) style =  style6;
    }

    /**
     * Shows overlay window
     *
     * @param roomToDisplay Unique ID of the room that is being used
     * @param roomWidth Room width for scaling purposes
     * @param roomHeight Room height for scaling purposes
     * @param roomX Room X position for scaling purposes
     * @param roomY Room Y position for scaling purposes
     * @param roomType Room type (sauna/regular)
     * @return Nothing
     */
    function showWindow (roomToDisplay, roomWidth, roomHeight, roomX, roomY, roomType) {
        roomAdjustWindowVisible = true
        mainStructure.iconHUDReload = false;
        customWindow.room = roomToDisplay;
        customScale_temp = 1 * (flickingArea.contentHeight / roomHeight);
        xpos_temp = (roomX * customScale_temp) - (flickingArea.width - roomWidth*customScale_temp)/2;
        ypos_temp = (roomY * customScale_temp);
        if (xpos_temp < 0 ) xpos_temp = 0;
        if ((xpos_temp+flickingArea.width) > (flickingArea.contentWidth*customScale_temp)) xpos_temp = (flickingArea.contentWidth * customScale_temp) - flickingArea.width;
        customScale = customScale_temp;
        xpos = xpos_temp;
        ypos = ypos_temp;
        customWindowArea.winTarget = roomType;
        customVisibility = 1;
        textCustomVisibility = 1;
        houseRoom1.hideHUD();
        houseRoom2.hideHUD();
        houseRoom3.hideHUD();
        houseRoom4.hideHUD();
        houseRoom5.hideHUD();
        houseRoom6.hideHUD();
        houseRoom7.hideHUD();
        houseRoom8.hideHUD();
    }

    /**
     * Hides overlay window
     *
     * @return Nothing
     */
    function hideWindow() {
        roomAdjustWindowVisible = false
        if (customWindowArea.winTarget == "RAW") HCS.saveRoomInfo(customWindow.room, customWindow.temp, customWindow.light, customWindow.volume, customWindow.mute);
        mainStructure.iconHUDReload = true;
        mainStructure.customScale = 1;
        xpos = 0;
        ypos = 0;
        customVisibility = 0;
        textCustomVisibility = 1;
        houseRoom1.hideBlur();
        houseRoom2.hideBlur();
        houseRoom3.hideBlur();
        houseRoom4.hideBlur();
        houseRoom5.hideBlur();
        houseRoom6.hideBlur();
        houseRoom7.hideBlur();
        houseRoom8.hideBlur();
    }

    Component.onCompleted: {
        startupFunction();
        changeCLStyle();
    }

    width: 800
    height: 424
    color: style.selectionColor

    Behavior on customScale { PropertyAnimation { target: mainStructure; property: "customScale"; duration: 500; easing.type: Easing.InOutQuad; } }
    Behavior on xpos { PropertyAnimation { target: mainStructure; property: "xpos"; duration: 500; easing.type: Easing.InOutQuad; } }
    Behavior on ypos { PropertyAnimation { target: mainStructure; property: "ypos"; duration: 500; easing.type: Easing.InOutQuad; } }
    Behavior on customVisibility { PropertyAnimation { target: mainStructure; property: "customVisibility"; duration: 300; easing.type: Easing.InOutQuad; } }
    Behavior on textCustomVisibility { PropertyAnimation { target: mainStructure; property: "textCustomVisibility"; duration: 300; easing.type: Easing.InOutQuad; } }


    Rectangle{
        anchors.fill:parent
        color:style.colorWhenActive
    }

    Image {
        source: "images/floorplan_bg.png"
        anchors.fill: parent
        smooth: true
    }

    Flickable {
        id: flickingArea
        width: parent.width-20
        height: parent.height-20
        contentWidth: Math.round(width * customScale)
        contentHeight: Math.round(height * customScale)
        contentX: mainStructure.xpos
        contentY: mainStructure.ypos
        anchors.verticalCenter: parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        interactive: false
        Rectangle {
            id: house;
            width: flickingArea.contentWidth
            height: flickingArea.contentHeight
            color: "transparent"
            anchors.left: parent.left
            anchors.top: parent.top

            TheRoom {
                id: houseRoom1
                style:mainStructure.style
                room: "room1"
                text: "Bedroom 1"
                width: Math.round(house.width * 0.2615)
                height: Math.ceil(house.height * (697/1350))
                y: Math.floor(house.height * (45/1350))
                x: Math.round(house.width * (51/2050))
                onClicked: showWindow(theRoomCode, houseRoom1.width, houseRoom1.height, houseRoom1.x, houseRoom1.y, "RAW")
            }

            TheRoom {
                id: houseRoom2
                style:mainStructure.style
                room: "room2"
                text: "Bedroom 2"
                height: Math.ceil(house.height * (563/1350))
                anchors.top: houseRoom1.bottom
                anchors.left: houseRoom1.left
                anchors.right: houseRoom1.right
                onClicked: showWindow(theRoomCode, houseRoom2.width, houseRoom2.height, houseRoom2.x, houseRoom2.y, "RAW")
            }

            TheRoom {
                id: houseRoom3
                style:mainStructure.style
                room: "kitchen"
                text: "Kitchen"
                width: Math.round(house.width * (873/2050))
                anchors.top: houseRoom1.top
                anchors.left: houseRoom1.right
                anchors.bottom: houseRoom1.bottom
                onClicked: showWindow(theRoomCode, houseRoom3.width, houseRoom3.height, houseRoom3.x, houseRoom3.y, "RAW")
            }

            TheRoom {
                id: houseRoom8
                style:mainStructure.style
                room: "livingroom"
                text: "Living room"
                anchors.top: houseRoom3.bottom
                anchors.left: houseRoom1.right
                anchors.bottom: houseRoom2.bottom
                anchors.right: houseRoom3.right
                onClicked: showWindow(theRoomCode, houseRoom8.width, houseRoom8.height, houseRoom8.x, houseRoom8.y, "RAW")
            }

            TheRoom {
                id: houseRoom4
                style:mainStructure.style
                room: "room3"
                text: "Master bedroom"
                width: Math.round(house.width * (541/2050))
                anchors.top: houseRoom1.bottom
                anchors.bottom: houseRoom2.bottom
                anchors.left: houseRoom3.right
                onClicked: showWindow(theRoomCode, houseRoom4.width, houseRoom4.height, houseRoom4.x, houseRoom4.y, "RAW")
            }

            TheRoom {
                id: houseRoom5
                style:mainStructure.style
                room: "khh"
                text: "Utility room"
                height: Math.round(house.height * (289/1350))
                anchors.top: houseRoom1.top
                anchors.left: houseRoom3.right
                anchors.right: houseRoom4.right
                onClicked: showWindow(theRoomCode, houseRoom5.width, houseRoom5.height, houseRoom5.x, houseRoom5.y, "RAW")
            }

            TheRoom {
                id: houseRoom6
                style:mainStructure.style
                room: "sauna"
                text: "Sauna"
                width: Math.round(house.width * (287/2050))
                anchors.top: houseRoom5.bottom
                anchors.left: houseRoom3.right
                anchors.bottom: houseRoom4.top
                onClicked: showWindow(theRoomCode, houseRoom6.width, houseRoom6.height, houseRoom6.x, houseRoom6.y, "SAUNA")
            }

            TheRoom {
                id: houseRoom7
                style:mainStructure.style
                room: "kph"
                text: "Bath"
                anchors.top: houseRoom5.bottom
                anchors.left: houseRoom6.right
                anchors.bottom: houseRoom4.top
                anchors.right: houseRoom5.right
                onClicked: showWindow(theRoomCode, houseRoom7.width, houseRoom7.height, houseRoom7.x, houseRoom7.y, "RAW")
            }

        }
        Image {
            id: bottomImg
            source: "images/floorplan.png"
            width: house.width
            height: house.height
            opacity: 1
        }
        LockHUD {
            anchors.bottom: bottomImg.bottom
            anchors.horizontalCenter: bottomImg.horizontalCenter
            anchors.bottomMargin: -5 * (flickingArea.height / 404)
            iconSize: 44 * (flickingArea.height / 404)
            doReload: mainStructure.iconHUDReload;
            opacity: textCustomVisibility
        }
    }

    Rectangle {
        id: customWindowArea

        property string winTarget: "SAUNA"

        color: "transparent"
        anchors.fill: parent
        visible: (mainStructure.customVisibility==0)?false:true;


        MouseArea {
            anchors.fill: parent
            onClicked: hideWindow()
        }

        RoomAdjustWidget {
            id: customWindow;
            style: mainStructure.style
            anchors.centerIn: parent;
            width: parent.width-120;
            height: parent.height-60;
            opacity: mainStructure.customVisibility
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            visible: (customWindowArea.winTarget=="RAW")?true:false
            onCloseWindow: hideWindow()
        }

        SaunaView{
            visible: (customWindowArea.winTarget=="SAUNA")?true:false
            style: mainStructure.style;
            width: parent.width*0.9;
            height: parent.height*0.9;
            anchors.centerIn: parent
            onCloseWindow: hideWindow()
        }
    }
    SideNavigation{id:sidenavigation; anchors.fill:parent; onThemeChange: mainStructure.changeCLStyle(); style: mainStructure.style; onProfileChanged: {customWindow.updateInfo();} }
}
